#include<iostream>
#include<cstdio>
#define N 8000
#define M 60000
#define K 31996000
#define W 140
#define R 60
#define R2 10
using namespace std;
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
int T,n,m,sn,cnt[M+1],sx[N+1],sy[N+1],sdelta[W+1][1<<R2],sdelta2[W+1][1<<R2],sdelta3[W+1][1<<R2],sdelta4[W+1][1<<R2],sdelta5[W+1][1<<R2],sdelta6[W+1][1<<R2],st[N+1][W+1],delta[N+1],num[N+1][N+1];
short X[K+1],Y[K+1];
long long ans;
unsigned long long ds[W+1],rst[N+1][W+1];
bool nprime[M+1],used[M+1];
int lowbit(int x)
{
	return x&(-x);
}
int dis(int x,int y)
{
	return abs(sx[x]-sx[y])+abs(sy[x]-sy[y]);
}
int calc(int x,unsigned long long d)
{
	return sdelta[x][d&1023]+sdelta2[x][(d>>10)&1023]+sdelta3[x][(d>>20)&1023]+sdelta4[x][(d>>30)&1023]+sdelta5[x][(d>>40)&1023]+sdelta6[x][d>>50];
}
int main()
{
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
	for (int i=2;i<=M;++i)
		if (!nprime[i])
		{
			used[i]=(i<=10||i%20==3);
			for (int j=(i<<1);j<=M;j+=i) nprime[j]=1;
		}
	T=read();
	while (T--)
	{
		for (int i=1;i<=M;++i) cnt[i]=0;
		n=read(),m=read(),ans=0;
		for (int i=1;i<=n;++i) sx[i]=read(),sy[i]=read();
		for (int i=1;i<=n;++i) delta[i]=read();
		for (int i=1;i<=n;++i)
			for (int j=i+1;j<=n;++j)
				num[i][j]=++cnt[dis(i,j)];
		for (int i=2;i<=M;++i) cnt[i]+=cnt[i-1];
		for (int i=1;i<=n;++i)
			for (int j=i+1;j<=n;++j)
				X[cnt[dis(i,j)-1]+num[i][j]]=i,Y[cnt[dis(i,j)-1]+num[i][j]]=j;
		sn=(n+R-1)/R;
		for (int i=1;i<=sn;++i)
		{
			//0
			for (int j=1;j<=R2;++j) sdelta[i][1<<(j-1)]=(i-1)*R+j<=n?delta[(i-1)*R+j]:0;
			for (int j=1;j<(1<<R2);++j) sdelta[i][j]=sdelta[i][j^lowbit(j)]+sdelta[i][lowbit(j)];
			//1
			for (int j=1;j<=R2;++j) sdelta2[i][1<<(j-1)]=(i-1)*R+10+j<=n?delta[(i-1)*R+10+j]:0;
			for (int j=1;j<(1<<R2);++j) sdelta2[i][j]=sdelta2[i][j^lowbit(j)]+sdelta2[i][lowbit(j)];
			//2
			for (int j=1;j<=R2;++j) sdelta3[i][1<<(j-1)]=(i-1)*R+20+j<=n?delta[(i-1)*R+20+j]:0;
			for (int j=1;j<(1<<R2);++j) sdelta3[i][j]=sdelta3[i][j^lowbit(j)]+sdelta3[i][lowbit(j)];
			//3
			for (int j=1;j<=R2;++j) sdelta4[i][1<<(j-1)]=(i-1)*R+30+j<=n?delta[(i-1)*R+30+j]:0;
			for (int j=1;j<(1<<R2);++j) sdelta4[i][j]=sdelta4[i][j^lowbit(j)]+sdelta4[i][lowbit(j)];
			//4
			for (int j=1;j<=R2;++j) sdelta5[i][1<<(j-1)]=(i-1)*R+40+j<=n?delta[(i-1)*R+40+j]:0;
			for (int j=1;j<(1<<R2);++j) sdelta5[i][j]=sdelta5[i][j^lowbit(j)]+sdelta5[i][lowbit(j)];
			//5
			for (int j=1;j<=R2;++j) sdelta6[i][1<<(j-1)]=(i-1)*R+50+j<=n?delta[(i-1)*R+50+j]:0;
			for (int j=1;j<(1<<R2);++j) sdelta6[i][j]=sdelta6[i][j^lowbit(j)]+sdelta6[i][lowbit(j)];
        }
		for (int i=1;i<=n;++i)
			for (int j=1;j<=sn;++j)
				rst[i][j]=0;
		for (int i=1;i<=((n*(n-1))>>1);++i)
		{
			if (used[dis(X[i],Y[i])])
			{
				for (int j=1;j<=sn;++j) ds[j]=rst[X[i]][j]^rst[Y[i]][j],ans+=__builtin_popcountll(ds[j])*(delta[X[i]]+delta[Y[i]])+calc(j,ds[j]);
			}
			rst[X[i]][(Y[i]-1)/R+1]|=(1ll<<((Y[i]-1)%R));
			rst[Y[i]][(X[i]-1)/R+1]|=(1ll<<((X[i]-1)%R));
		}
		printf("%lld\n",ans);
	}
	return 0;
}
